library(patchwork)
library(tidyverse)

DATA         <- tribble(
  ~STEREOTYPE, ~GROUP, ~RATING, ~PE,
  "Hard-working", "Ratings\nabout\nWhites"              , "Missing", 0.0018927,
  "Hard-working", "Ratings\nabout\nWhites"              , "1"      , 0.0227129,
  "Hard-working", "Ratings\nabout\nWhites"              , "2"      , 0.0384858,
  "Hard-working", "Ratings\nabout\nWhites"              , "3"      , 0.0977918,
  "Hard-working", "Ratings\nabout\nWhites"              , "4"      , 0.2485804,
  "Hard-working", "Ratings\nabout\nWhites"              , "5"      , 0.2126183,
  "Hard-working", "Ratings\nabout\nWhites"              , "6"      , 0.1867508,
  "Hard-working", "Ratings\nabout\nWhites"              , "7"      , 0.1911672,
  "Hard-working", "Ratings\nabout\nBlacks"              , "Missing", 0.0018927,
  "Hard-working", "Ratings\nabout\nBlacks"              , "1"      , 0.0391167,
  "Hard-working", "Ratings\nabout\nBlacks"              , "2"      , 0.051735 ,
  "Hard-working", "Ratings\nabout\nBlacks"              , "3"      , 0.1116719,
  "Hard-working", "Ratings\nabout\nBlacks"              , "4"      , 0.2599369,
  "Hard-working", "Ratings\nabout\nBlacks"              , "5"      , 0.1791798,
  "Hard-working", "Ratings\nabout\nBlacks"              , "6"      , 0.155836 ,
  "Hard-working", "Ratings\nabout\nBlacks"              , "7"      , 0.2006309,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "Missing", 0.0025237,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "1"      , 0.0157729,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "2"      , 0.0227129,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "3"      , 0.0586751,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "4"      , 0.1804416,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "5"      , 0.1602524,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "6"      , 0.2321767,
  "Hard-working", "Ratings\nabout\nHispanic-\nAmericans", "7"      , 0.3274448,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "Missing", 0.0018927,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "1"      , 0.0138801,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "2"      , 0.0195584,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "3"      , 0.0397476,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "4"      , 0.1785489,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "5"      , 0.1362776,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "6"      , 0.2309148,
  "Hard-working", "Ratings\nabout\nAsian-\nAmericans"   , "7"      , 0.3791798,
  "Intelligence", "Ratings\nabout\nWhites"              , "Missing", 0.0012618,
  "Intelligence", "Ratings\nabout\nWhites"              , "1"      , 0.0189274,
  "Intelligence", "Ratings\nabout\nWhites"              , "2"      , 0.0189274,
  "Intelligence", "Ratings\nabout\nWhites"              , "3"      , 0.0523659,
  "Intelligence", "Ratings\nabout\nWhites"              , "4"      , 0.2700315,
  "Intelligence", "Ratings\nabout\nWhites"              , "5"      , 0.2018927,
  "Intelligence", "Ratings\nabout\nWhites"              , "6"      , 0.2302839,
  "Intelligence", "Ratings\nabout\nWhites"              , "7"      , 0.2063091,
  "Intelligence", "Ratings\nabout\nBlacks"              , "Missing", 0.0018927,
  "Intelligence", "Ratings\nabout\nBlacks"              , "1"      , 0.0239748,
  "Intelligence", "Ratings\nabout\nBlacks"              , "2"      , 0.0334385,
  "Intelligence", "Ratings\nabout\nBlacks"              , "3"      , 0.0832808,
  "Intelligence", "Ratings\nabout\nBlacks"              , "4"      , 0.2977918,
  "Intelligence", "Ratings\nabout\nBlacks"              , "5"      , 0.1791798,
  "Intelligence", "Ratings\nabout\nBlacks"              , "6"      , 0.1728707,
  "Intelligence", "Ratings\nabout\nBlacks"              , "7"      , 0.207571 ,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "Missing", 0.0018927,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "1"      , 0.0170347,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "2"      , 0.0258675,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "3"      , 0.0700315,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "4"      , 0.2921136,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "5"      , 0.1955836,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "6"      , 0.1943218,
  "Intelligence", "Ratings\nabout\nHispanic-\nAmericans", "7"      , 0.2031546,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "Missing", 0.0025237,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "1"      , 0.0094637,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "2"      , 0.0201893,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "3"      , 0.0309148,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "4"      , 0.2082019,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "5"      , 0.1419558,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "6"      , 0.2391167,
  "Intelligence", "Ratings\nabout\nAsian-\nAmericans"   , "7"      , 0.3476341)

DATA$GROUP   <- factor(DATA$GROUP , levels = unique(DATA$GROUP))
DATA$RATING  <- factor(DATA$RATING, levels = unique(DATA$RATING))

DATA.WORK    <- filter(DATA, STEREOTYPE == "Hard-working")
DATA.INTEL   <- filter(DATA, STEREOTYPE == "Intelligence")

theme.z <-   theme(
  axis.text.x        = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8, b = 0)),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8, b = 0)),
  axis.text.y        = element_text(size = 15, color = "black", hjust = 1  , vjust=0.5, margin = margin(l = 8, r = 8)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , margin = margin(l = 8, r = 8)),
  axis.ticks.x       = element_blank(),
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_blank(), 
  axis.title.y       = element_blank(),
  legend.position    = "none",
  panel.background   = element_blank(), 
  panel.border       = element_blank(), 
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(), 
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(2, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12 , hjust = 0  , margin = margin(t = 10)),
  plot.margin        = unit(c(t = 5, r = 2, b = 5, l = 2),"pt"),
  plot.subtitle      = element_text(size = 15 , hjust = 0.5, margin = margin(b = 10)),
  plot.title         = element_text(size = 17 , hjust = 0.5, margin = margin(t = 0, b = 10), face = "bold"),
)

COLOR1 <- "darkseagreen4"
COLOR2 <- "darkseagreen3"
COLOR3 <- "darkseagreen1"
COLOR4 <- "gray90"
COLOR5 <- "lightpink1"
COLOR6 <- "indianred2"
COLOR7 <- "indianred4"
COLOR8 <- "white"

p1 <- ggplot(DATA.WORK, aes(fill = RATING, y = 100*PE, x = GROUP)) +
  geom_col(color = "black", linewidth = 0.75, width = 1) +
  geom_rect(aes(xmin = 0.5, xmax = 4.5, ymin = 0, ymax = 100), fill = NA, col = "black", linewidth = 2) +
  scale_fill_manual(values = rev(rep.int(c(COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8), 4))) +
  scale_y_continuous(limits = c(0,100.1), position = "left", breaks=c(9.6, 28.4, 48.4, 71.5, 88.8, 95, 98.7), label=c("Hard-working 1","2","3","4","5","6","Lazy 7"), expand=c(0,0), sec.axis = sec_axis(~., breaks = seq(0,100,10), label = c("\u2014",rep.int("\u2013",4),"\u2014",rep.int("\u2013",4),"\u2014"))) +
  scale_x_discrete(expand = c(0,0)) +
  theme.z 

p2 <- ggplot(DATA.INTEL, aes(fill = RATING, y = 100*PE, x = GROUP)) +
  geom_col(color = "black", linewidth = 0.75, width = 1) +
  geom_rect(aes(xmin = 0.5, xmax = 4.5, ymin = 0, ymax = 100), fill = NA, col = "black", linewidth = 2) +
  scale_fill_manual(values = rev(rep.int(c(COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8), 4))) +
  scale_y_continuous(limits = c(0,100.1), position = "left", expand=c(0,0), breaks = seq(0,100,10), label = c("\u2014",rep.int("\u2013",4),"\u2014",rep.int("\u2013",4),"\u2014"), sec.axis = sec_axis(~., breaks=c(17.4, 46.7, 65.8, 83.3, 92, 96, 100), label=c("1 Intelligent","2","3","4","5","6","7 Unintelligent"))) +
  scale_x_discrete(expand = c(0,0)) +
  theme.z 

p1 + p2

ggsave(file = "Stereotype ordering Figure A1.svg", width = 14, height = 6.5)
